//
//  MYBottomPopView.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/11/6.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit

class MYBottomPopView: UIView,UIGestureRecognizerDelegate {
    var contentView:UIView?;
    
    class func showWithContentView(contentView:UIView)->Void{
        let bottomView = MYBottomPopView.init();
        bottomView.showWithContentView(contentView: contentView, pView: ((UIApplication.shared.delegate?.window)!)!);
    }
    class func dismiss()->Void{
        let window = UIApplication.shared.delegate?.window;
        let bottomView = window??.viewWithTag(-1000) as! MYBottomPopView;
        bottomView.dismiss();
    }
    func showWithContentView(contentView:UIView, pView:UIView) -> Void {
        tag = -1000;
        frame = pView.bounds;
        backgroundColor = UIColor.clear;
//        addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(dimissPressed(ges: ))));
        self.contentView = contentView;
        let height = self.contentView!.frame.size.height;
        self.contentView?.frame = CGRect.init(x: 0, y: kHeight, width: kWidth, height: kHeight);
        pView.addSubview(self);
        self.addSubview(self.contentView!);
        UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.init(rawValue: 7), animations: {
            self.contentView?.frame = CGRect.init(x: 0, y: kHeight-height, width: kWidth, height: height);
            self.backgroundColor = UIColor.init(white: 0, alpha: 0.2);
        }) { (finished) in
            
        }
    }
    @objc func dimissPressed(ges:UITapGestureRecognizer){
        let point = ges.location(in: self);
        if point.y < (contentView?.frame.origin.y)! {
            dismiss();
        }
    }
    func dismiss() -> Void {
        UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.init(rawValue: 7), animations: {
            let height = self.contentView!.frame.size.height;
            self.contentView?.frame = CGRect.init(x: 0, y: kHeight, width: kWidth, height: height);
            self.backgroundColor = UIColor.clear;
            
        }) { (finished) in
            self.removeFromSuperview();
        }
    }
}
